<!DOCTYPE html>
<html>
<head>
    {include file="layout:_meta_login" /}
    <meta name="csrf-token" content="{:token()}">
    <link rel="stylesheet" href="__static__/admin/css/login.css" media="all">
</head>
<body>
<div class="layui-container">
    <div class="admin-login-background">
        <div class="layui-form login-form">
            <div class="layui-form">
                <div class="layui-form-item logo-title">
                    <h1>{:SysConfig('site.login_title')}</h1>
                    <h1>{:SysConfig('site.title')}</h1>
                </div>
                <div class="layui-form-item">
                    <label class="layui-icon layui-icon-username" for="login-username"></label>
                    <input type="text" id="login-username" name="username" lay-verify="required|username" placeholder="用户名或者邮箱" autocomplete="off" class="layui-input" >
                </div>
                <div class="layui-form-item">
                    <label class="layui-icon layui-icon-password" for="login-password"></label>
                    <input type="password" id="login-password" name="password" lay-verify="required|password" placeholder="密码" autocomplete="off" class="layui-input" >
                </div>
                <div class="layui-form-item">
                    <label class="layui-icon layui-icon-vercode" for="login-vercode"></label>
                    <input type="text" id="login-vercode" name="verify" lay-verify="required|captcha" placeholder="图形验证码" autocomplete="off" class="layui-input verification captcha" >
                    <div class="captcha-img">
                        <img id="get-vercode" src="{:captcha_src('login')}" onclick="this.src='{:captcha_src(\'login\')}?'+Math.random();" >
                    </div>
                </div>
                <div class="layui-form-item">
                    <input type="checkbox" id="login-remember" name="remember"  lay-skin="primary" title="记住密码">
                </div>
                <div class="layui-form-item">
                    <button id="login-submit" class="layui-btn layui-btn layui-btn-normal layui-btn-fluid" lay-submit lay-filter="login-submit">登 入</button>
                </div>
            </div>
        </div>
    </div>
</div>
<div style="text-align:center"><a target="_blank" href="https://beian.miit.gov.cn/">{:SysConfig('site.icp')}</a></div>
{include file="layout:_footer" /}
<script src="__static__/admin/js/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="__static__/admin/js/jquery.particleground.min.js" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var $ = layui.$,
            form = layui.form,
            layer = layui.layer;

        // 登录过期的时候，跳出ifram框架
        if (top.location != self.location) top.location = self.location;

        // 粒子线条背景
        $(document).ready(function(){
            $('.layui-container').particleground({
                dotColor:'#7ec7fd',
                lineColor:'#7ec7fd'
            });
        });

        //增加csrf验证
        $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});

        //是否记住密码
        $.post( "{:url('index/remember')}",function(data) {
            if (data.data.username) {
                $('#login-username').val(data.data.username);
                $('#login-password').val(data.data.password);
                $('#login-remember').attr('checked', true);
                form.render();
            }
        });

        //表单验证
        form.verify({
            username: function (value, item) { //value：表单的值、item：表单的DOM对象
                let msg;
                $.ajax({
                    async: false,
                    type:'post',
                    url: "{:url('index/check')}",
                    data: {username: value},
                    success: function (data) {
                        if (data.code==0) {
                            msg = data.msg;
                        }
                    }
                });
                return msg;
            },

            password: [
                /^[\S]{6,16}$/
                ,'请输入6到16位的密码'
            ],
        });

        //回车监听
        document.onkeydown = function (event) {
            var e = event || window.event;
            if (e && e.keyCode == 13) { //回车键的键值为13
                $('#login-submit').click();
            }
        };

        //提交
        form.on('submit(login-submit)', function(obj){
            $.post("{:url('index/login')}", obj.field, function (res) {
                var icon = 5;
                if (res.code) {
                    icon = 6;
                }
                layer.msg(res.msg, {icon: icon, time: 1500}, function () {   //提示的插件，可以使用alert代替
                    if (res.code) {
                        setTimeout(function () {
                            location.href="{:url('index/index')}";
                        }, 500);
                    } else {
                        $('#get-vercode').click();
                        //刷新csrf验证
                        $.ajaxSetup({headers: {'X-CSRF-TOKEN': res.data.token}});
                    }
                });
            }, 'json');
            return false;
        });

    });
</script>
</body>
</html>
